package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTakeDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.FodderTake;
import cn.edu.scau.cmi.hombio.domain.FodderType;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for FodderTake entities
 * 
 */

@Service("FodderTakeService")

@Transactional
public class FodderTakeServiceImpl implements FodderTakeService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages FodderTake entities
	 * 
	 */
	@Autowired
	private FodderTakeDAO fodderTakeDAO;

	/**
	 * DAO injected by Spring that manages FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeDAO fodderTypeDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new FodderTakeServiceImpl.
	 *
	 */
	public FodderTakeServiceImpl() {
	}

	/**
	 * Delete an existing FodderType entity
	 * 
	 */
	@Transactional
	public FodderTake deleteFodderTakeFodderType(Integer foddertake_id, Integer related_foddertype_id) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(foddertake_id, -1, -1);
		FodderType related_foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertype_id, -1, -1);

		foddertake.setFodderType(null);
		related_foddertype.getFodderTakes().remove(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_foddertype = fodderTypeDAO.store(related_foddertype);
		fodderTypeDAO.flush();

		fodderTypeDAO.remove(related_foddertype);
		fodderTypeDAO.flush();

		return foddertake;
	}

	/**
	 * Load an existing FodderTake entity
	 * 
	 */
	@Transactional
	public Set<FodderTake> loadFodderTakes() {
		return fodderTakeDAO.findAllFodderTakes();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public FodderTake deleteFodderTakeTask(Integer foddertake_id, Integer related_task_id) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(foddertake_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		foddertake.setTask(null);
		related_task.getFodderTakes().remove(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return foddertake;
	}

	/**
	 */
	@Transactional
	public FodderTake findFodderTakeByPrimaryKey(Integer id) {
		return fodderTakeDAO.findFodderTakeByPrimaryKey(id);
	}

	/**
	 * Save an existing FodderType entity
	 * 
	 */
	@Transactional
	public FodderTake saveFodderTakeFodderType(Integer id, FodderType related_foddertype) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(id, -1, -1);
		FodderType existingfodderType = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderType != null) {
			existingfodderType.setId(related_foddertype.getId());
			existingfodderType.setName(related_foddertype.getName());
			existingfodderType.setSupply(related_foddertype.getSupply());
			existingfodderType.setMemo(related_foddertype.getMemo());
			related_foddertype = existingfodderType;
		}

		foddertake.setFodderType(related_foddertype);
		related_foddertype.getFodderTakes().add(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_foddertype = fodderTypeDAO.store(related_foddertype);
		fodderTypeDAO.flush();

		return foddertake;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public FodderTake saveFodderTakeEmployeeByWarehousekepperId(Integer id, Employee related_employeebywarehousekepperid) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(id, -1, -1);
		Employee existingemployeeByWarehousekepperId = employeeDAO.findEmployeeByPrimaryKey(related_employeebywarehousekepperid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployeeByWarehousekepperId != null) {
			existingemployeeByWarehousekepperId.setId(related_employeebywarehousekepperid.getId());
			existingemployeeByWarehousekepperId.setNumber(related_employeebywarehousekepperid.getNumber());
			existingemployeeByWarehousekepperId.setName(related_employeebywarehousekepperid.getName());
			existingemployeeByWarehousekepperId.setPassword(related_employeebywarehousekepperid.getPassword());
			existingemployeeByWarehousekepperId.setLogOn(related_employeebywarehousekepperid.getLogOn());
			existingemployeeByWarehousekepperId.setRfid(related_employeebywarehousekepperid.getRfid());
			existingemployeeByWarehousekepperId.setSex(related_employeebywarehousekepperid.getSex());
			existingemployeeByWarehousekepperId.setBirthday(related_employeebywarehousekepperid.getBirthday());
			existingemployeeByWarehousekepperId.setState(related_employeebywarehousekepperid.getState());
			existingemployeeByWarehousekepperId.setMemo(related_employeebywarehousekepperid.getMemo());
			related_employeebywarehousekepperid = existingemployeeByWarehousekepperId;
		} else {
			related_employeebywarehousekepperid = employeeDAO.store(related_employeebywarehousekepperid);
			employeeDAO.flush();
		}

		foddertake.setEmployeeByWarehousekepperId(related_employeebywarehousekepperid);
		related_employeebywarehousekepperid.getFodderTakesForWarehousekepperId().add(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_employeebywarehousekepperid = employeeDAO.store(related_employeebywarehousekepperid);
		employeeDAO.flush();

		return foddertake;
	}

	/**
	 * Save an existing FodderTake entity
	 * 
	 */
	@Transactional
	public void saveFodderTake(FodderTake foddertake) {
		FodderTake existingFodderTake = fodderTakeDAO.findFodderTakeByPrimaryKey(foddertake.getId());

		if (existingFodderTake != null) {
			if (existingFodderTake != foddertake) {
				existingFodderTake.setId(foddertake.getId());
				existingFodderTake.setQuantity(foddertake.getQuantity());
				existingFodderTake.setTime(foddertake.getTime());
				existingFodderTake.setMemo(foddertake.getMemo());
			}
			foddertake = fodderTakeDAO.store(existingFodderTake);
		} else {
			foddertake = fodderTakeDAO.store(foddertake);
		}
		fodderTakeDAO.flush();
	}

	/**
	 * Return a count of all FodderTake entity
	 * 
	 */
	@Transactional
	public Integer countFodderTakes() {
		return ((Long) fodderTakeDAO.createQuerySingleResult("select count(o) from FodderTake o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public FodderTake deleteFodderTakeEmployeeByWarehousekepperId(Integer foddertake_id, Integer related_employeebywarehousekepperid_id) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(foddertake_id, -1, -1);
		Employee related_employeebywarehousekepperid = employeeDAO.findEmployeeByPrimaryKey(related_employeebywarehousekepperid_id, -1, -1);

		foddertake.setEmployeeByWarehousekepperId(null);
		related_employeebywarehousekepperid.getFodderTakesForWarehousekepperId().remove(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_employeebywarehousekepperid = employeeDAO.store(related_employeebywarehousekepperid);
		employeeDAO.flush();

		employeeDAO.remove(related_employeebywarehousekepperid);
		employeeDAO.flush();

		return foddertake;
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public FodderTake deleteFodderTakeEmployeeByEmployeeId(Integer foddertake_id, Integer related_employeebyemployeeid_id) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(foddertake_id, -1, -1);
		Employee related_employeebyemployeeid = employeeDAO.findEmployeeByPrimaryKey(related_employeebyemployeeid_id, -1, -1);

		foddertake.setEmployeeByEmployeeId(null);
		related_employeebyemployeeid.getFodderTakesForEmployeeId().remove(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_employeebyemployeeid = employeeDAO.store(related_employeebyemployeeid);
		employeeDAO.flush();

		employeeDAO.remove(related_employeebyemployeeid);
		employeeDAO.flush();

		return foddertake;
	}

	/**
	 * Delete an existing FodderTake entity
	 * 
	 */
	@Transactional
	public void deleteFodderTake(FodderTake foddertake) {
		fodderTakeDAO.remove(foddertake);
		fodderTakeDAO.flush();
	}

	/**
	 * Return all FodderTake entity
	 * 
	 */
	@Transactional
	public List<FodderTake> findAllFodderTakes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<FodderTake>(fodderTakeDAO.findAllFodderTakes(startResult, maxRows));
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public FodderTake saveFodderTakeTask(Integer id, Task related_task) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		foddertake.setTask(related_task);
		related_task.getFodderTakes().add(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return foddertake;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public FodderTake saveFodderTakeEmployeeByEmployeeId(Integer id, Employee related_employeebyemployeeid) {
		FodderTake foddertake = fodderTakeDAO.findFodderTakeByPrimaryKey(id, -1, -1);
		Employee existingemployeeByEmployeeId = employeeDAO.findEmployeeByPrimaryKey(related_employeebyemployeeid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployeeByEmployeeId != null) {
			existingemployeeByEmployeeId.setId(related_employeebyemployeeid.getId());
			existingemployeeByEmployeeId.setNumber(related_employeebyemployeeid.getNumber());
			existingemployeeByEmployeeId.setName(related_employeebyemployeeid.getName());
			existingemployeeByEmployeeId.setPassword(related_employeebyemployeeid.getPassword());
			existingemployeeByEmployeeId.setLogOn(related_employeebyemployeeid.getLogOn());
			existingemployeeByEmployeeId.setRfid(related_employeebyemployeeid.getRfid());
			existingemployeeByEmployeeId.setSex(related_employeebyemployeeid.getSex());
			existingemployeeByEmployeeId.setBirthday(related_employeebyemployeeid.getBirthday());
			existingemployeeByEmployeeId.setState(related_employeebyemployeeid.getState());
			existingemployeeByEmployeeId.setMemo(related_employeebyemployeeid.getMemo());
			related_employeebyemployeeid = existingemployeeByEmployeeId;
		}

		foddertake.setEmployeeByEmployeeId(related_employeebyemployeeid);
		related_employeebyemployeeid.getFodderTakesForEmployeeId().add(foddertake);
		foddertake = fodderTakeDAO.store(foddertake);
		fodderTakeDAO.flush();

		related_employeebyemployeeid = employeeDAO.store(related_employeebyemployeeid);
		employeeDAO.flush();

		return foddertake;
	}
}
